This is an casting page of testing the efficiency of the coding and also some useful r packages for the research on Betting Strategy and Model Validation. Some packages are introduce from other websites while I made it a scrapbook to easier for references.
## Setup Options, Loading Required Libraries and Preparing Environment
## Loading the packages and setting adjustment
suppressMessages(library('BBmisc'))
suppressAll(library('utils'))
suppressAll(source('./function/libs.R', local = TRUE))## Load saved dataset to save the loading time.
## directly load the dataset from running chunk `read-data-summary-table` and also chunk `scrap-data`.
## The spboData for filtering leagues and matches scores purpose. Kindly refer to file named
## `compacted data - shinyData.txt` inside folder `data`.
## Run above codes and save.images() and now directly load for shinyApp use.
load('./regressionApps/shinyData.RData', envir = .GlobalEnv)
## -------- chunk `bank-roll` -----------
## Re-categorise the soccer financial settlement date. Due to I have no the history matches dataset from bookmakers. The scrapped spbo time is not stable (always change, moreover there just an information website) where firm A is the firm who placed bets with millions HKD (although the kick-off time might also changed after placed that particular bet), therefore I follow the kick-off time of the firm A.
#'@ dat <- dat[order(dat$DateUK),] %>% mutate(DateUS = as.Date(format(DateUK, tz = 'EST', usetz = TRUE, format = '%Y-%m-%d %H:%M:%S'))) #daily settlement will base on variable `DateUS`.In my previous assignments, there has some useful function for read files :
scan() - scan the table files.readLines() - read the files line by line.readtable() - read the table files.readBin() - fatest since read the binary code and convert to character.
Below is codes for merge files, there has another function which is plyr::join_all() which is very efficient, however the datetime format will became 0 value but reshape::merge_all() can combine the data efficiently but taken longer time.
nrow(do.call(rbind, llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = TRUE)))## [1] 48744
nrow(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = TRUE)))## [1] 48744
system.time(do.call(rbind, llply(as.list(seq(2011,2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = TRUE)))## user system elapsed
## 0.12 0.02 0.59
system.time(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, read.csv(paste0('./data/', x, '.csv'))), .parallel = TRUE)))## user system elapsed
## 0.06 0.02 0.50
system.time(bind_rows(llply(as.list(seq(2011, 2015)), function(x) data.frame(Sess = x, data.table::fread(paste0('./data/', x, '.csv'))), .parallel = TRUE)))## user system elapsed
## 0.11 0.01 0.53
You can also embed plots, for example:
#'@ system.time(Reduce(function(x,y) {merge(x,y,all=TRUE)}, llply(list(df1,df1.sps,df1.pst),function(x) x[[1]])))
#'@ system.time(merge_all(list(df1[[1]],df1.sps[[1]],df1.pst[[1]])))Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
#'@ system.time(merge(socData, othData, all=TRUE))
#'@ system.time(merge_all(list(socData, othData)))There are quite some r packages for ploting html table :
suppressMessages(library('knitr'))
suppressMessages(library('xtable'))
suppressMessages(library('htmlTable'))
suppressMessages(library('plyr'))
suppressMessages(library('dplyr'))
suppressMessages(library('formattable'))
## Re-categorise the soccer financial settlement date. Due to I have no the history matches dataset from bookmakers. The scrapped spbo time is not stable (always change, moreover there just an information website) where firm A is the firm who placed bets with millions HKD (although the kick-off time might also changed after placed that particular bet), therefore I follow the kick-off time of the firm A.
dat <- dat[order(dat$DateUK),] %>% mutate(DateUS = as.Date(format(DateUK, tz = 'EST', usetz = TRUE, format = '%Y-%m-%d %H:%M:%S'))) #daily settlement will base on variable `DateUS`.
BR <- ddply(dat, .(DateUS), summarise, Stakes = sum(Stakes), Return = sum(Return), PL = sum(PL), n = length(Sess), rRates = Return / Stakes) %>% mutate(CumStakes = cumsum(Stakes), SPL = cumsum(PL), BR = 4600 + SPL, gRates = BR/CumStakes)
summary(BR)## DateUS Stakes Return PL
## Min. :2011-01-07 Min. : 4 Min. : 0.0 Min. :-2527.41
## 1st Qu.:2012-04-01 1st Qu.: 328 1st Qu.: 293.5 1st Qu.: -126.69
## Median :2013-06-04 Median : 759 Median : 773.2 Median : 14.39
## Mean :2013-05-13 Mean :1155 Mean :1179.4 Mean : 24.82
## 3rd Qu.:2014-06-29 3rd Qu.:1520 3rd Qu.:1584.4 3rd Qu.: 182.97
## Max. :2015-07-19 Max. :6108 Max. :6613.1 Max. : 3005.05
## n rRates CumStakes SPL
## Min. : 1.00 Min. :0.0000 Min. : 40 Min. :-4587
## 1st Qu.: 9.00 1st Qu.:0.8128 1st Qu.: 384506 1st Qu.:10615
## Median : 19.00 Median :1.0240 Median : 827028 Median :19262
## Mean : 28.49 Mean :1.0341 Mean : 815158 Mean :17774
## 3rd Qu.: 38.00 3rd Qu.:1.2596 3rd Qu.:1209336 3rd Qu.:24086
## Max. :148.00 Max. :2.2033 Max. :1663757 Max. :38113
## BR gRates
## Min. : 13.05 Min. : 0.00008
## 1st Qu.:15215.39 1st Qu.: 0.02431
## Median :23862.28 Median : 0.02683
## Mean :22373.80 Mean : 0.12675
## 3rd Qu.:28686.30 3rd Qu.: 0.03405
## Max. :42712.54 Max. :114.00000
summary(BR) %>% formattable ## DateUS Stakes Return
## "Min. :2011-01-07 " "Min. : 4 " "Min. : 0.0 "
## "1st Qu.:2012-04-01 " "1st Qu.: 328 " "1st Qu.: 293.5 "
## "Median :2013-06-04 " "Median : 759 " "Median : 773.2 "
## "Mean :2013-05-13 " "Mean :1155 " "Mean :1179.4 "
## "3rd Qu.:2014-06-29 " "3rd Qu.:1520 " "3rd Qu.:1584.4 "
## "Max. :2015-07-19 " "Max. :6108 " "Max. :6613.1 "
## PL n rRates
## "Min. :-2527.41 " "Min. : 1.00 " "Min. :0.0000 "
## "1st Qu.: -126.69 " "1st Qu.: 9.00 " "1st Qu.:0.8128 "
## "Median : 14.39 " "Median : 19.00 " "Median :1.0240 "
## "Mean : 24.82 " "Mean : 28.49 " "Mean :1.0341 "
## "3rd Qu.: 182.97 " "3rd Qu.: 38.00 " "3rd Qu.:1.2596 "
## "Max. : 3005.05 " "Max. :148.00 " "Max. :2.2033 "
## CumStakes SPL BR
## "Min. : 40 " "Min. :-4587 " "Min. : 13.05 "
## "1st Qu.: 384506 " "1st Qu.:10615 " "1st Qu.:15215.39 "
## "Median : 827028 " "Median :19262 " "Median :23862.28 "
## "Mean : 815158 " "Mean :17774 " "Mean :22373.80 "
## "3rd Qu.:1209336 " "3rd Qu.:24086 " "3rd Qu.:28686.30 "
## "Max. :1663757 " "Max. :38113 " "Max. :42712.54 "
## gRates
## "Min. : 0.00008 "
## "1st Qu.: 0.02431 "
## "Median : 0.02683 "
## "Mean : 0.12675 "
## "3rd Qu.: 0.03405 "
## "Max. :114.00000 "
summary(BR) %>% formattable %>% as.htmlwidgetkable(summary(BR))| DateUS | Stakes | Return | PL | n | rRates | CumStakes | SPL | BR | gRates | |
|---|---|---|---|---|---|---|---|---|---|---|
| Min. :2011-01-07 | Min. : 4 | Min. : 0.0 | Min. :-2527.41 | Min. : 1.00 | Min. :0.0000 | Min. : 40 | Min. :-4587 | Min. : 13.05 | Min. : 0.00008 | |
| 1st Qu.:2012-04-01 | 1st Qu.: 328 | 1st Qu.: 293.5 | 1st Qu.: -126.69 | 1st Qu.: 9.00 | 1st Qu.:0.8128 | 1st Qu.: 384506 | 1st Qu.:10615 | 1st Qu.:15215.39 | 1st Qu.: 0.02431 | |
| Median :2013-06-04 | Median : 759 | Median : 773.2 | Median : 14.39 | Median : 19.00 | Median :1.0240 | Median : 827028 | Median :19262 | Median :23862.28 | Median : 0.02683 | |
| Mean :2013-05-13 | Mean :1155 | Mean :1179.4 | Mean : 24.82 | Mean : 28.49 | Mean :1.0341 | Mean : 815158 | Mean :17774 | Mean :22373.80 | Mean : 0.12675 | |
| 3rd Qu.:2014-06-29 | 3rd Qu.:1520 | 3rd Qu.:1584.4 | 3rd Qu.: 182.97 | 3rd Qu.: 38.00 | 3rd Qu.:1.2596 | 3rd Qu.:1209336 | 3rd Qu.:24086 | 3rd Qu.:28686.30 | 3rd Qu.: 0.03405 | |
| Max. :2015-07-19 | Max. :6108 | Max. :6613.1 | Max. : 3005.05 | Max. :148.00 | Max. :2.2033 | Max. :1663757 | Max. :38113 | Max. :42712.54 | Max. :114.00000 |
htmlTable(summary(BR))
|
|
|
|
|
| CumStakes |
|
|
| |
|---|---|---|---|---|---|---|---|---|---|---|
| Min. :2011-01-07 | Min. : 4 | Min. : 0.0 | Min. :-2527.41 | Min. : 1.00 | Min. :0.0000 | Min. : 40 | Min. :-4587 | Min. : 13.05 | Min. : 0.00008 | |
| 1st Qu.:2012-04-01 | 1st Qu.: 328 | 1st Qu.: 293.5 | 1st Qu.: -126.69 | 1st Qu.: 9.00 | 1st Qu.:0.8128 | 1st Qu.: 384506 | 1st Qu.:10615 | 1st Qu.:15215.39 | 1st Qu.: 0.02431 | |
| Median :2013-06-04 | Median : 759 | Median : 773.2 | Median : 14.39 | Median : 19.00 | Median :1.0240 | Median : 827028 | Median :19262 | Median :23862.28 | Median : 0.02683 | |
| Mean :2013-05-13 | Mean :1155 | Mean :1179.4 | Mean : 24.82 | Mean : 28.49 | Mean :1.0341 | Mean : 815158 | Mean :17774 | Mean :22373.80 | Mean : 0.12675 | |
| 3rd Qu.:2014-06-29 | 3rd Qu.:1520 | 3rd Qu.:1584.4 | 3rd Qu.: 182.97 | 3rd Qu.: 38.00 | 3rd Qu.:1.2596 | 3rd Qu.:1209336 | 3rd Qu.:24086 | 3rd Qu.:28686.30 | 3rd Qu.: 0.03405 | |
| Max. :2015-07-19 | Max. :6108 | Max. :6613.1 | Max. : 3005.05 | Max. :148.00 | Max. :2.2033 | Max. :1663757 | Max. :38113 | Max. :42712.54 | Max. :114.00000 |
print(xtable(summary(BR)), type = 'html')## <!-- html table generated in R 3.3.2 by xtable 1.8-2 package -->
## <!-- Tue Dec 27 17:34:54 2016 -->
## <table border=1>
## <tr> <th> </th> <th> DateUS </th> <th> Stakes </th> <th> Return </th> <th> PL </th> <th> n </th> <th> rRates </th> <th> CumStakes </th> <th> SPL </th> <th> BR </th> <th> gRates </th> </tr>
## <tr> <td align="right"> 1 </td> <td> Min. :2011-01-07 </td> <td> Min. : 4 </td> <td> Min. : 0.0 </td> <td> Min. :-2527.41 </td> <td> Min. : 1.00 </td> <td> Min. :0.0000 </td> <td> Min. : 40 </td> <td> Min. :-4587 </td> <td> Min. : 13.05 </td> <td> Min. : 0.00008 </td> </tr>
## <tr> <td align="right"> 2 </td> <td> 1st Qu.:2012-04-01 </td> <td> 1st Qu.: 328 </td> <td> 1st Qu.: 293.5 </td> <td> 1st Qu.: -126.69 </td> <td> 1st Qu.: 9.00 </td> <td> 1st Qu.:0.8128 </td> <td> 1st Qu.: 384506 </td> <td> 1st Qu.:10615 </td> <td> 1st Qu.:15215.39 </td> <td> 1st Qu.: 0.02431 </td> </tr>
## <tr> <td align="right"> 3 </td> <td> Median :2013-06-04 </td> <td> Median : 759 </td> <td> Median : 773.2 </td> <td> Median : 14.39 </td> <td> Median : 19.00 </td> <td> Median :1.0240 </td> <td> Median : 827028 </td> <td> Median :19262 </td> <td> Median :23862.28 </td> <td> Median : 0.02683 </td> </tr>
## <tr> <td align="right"> 4 </td> <td> Mean :2013-05-13 </td> <td> Mean :1155 </td> <td> Mean :1179.4 </td> <td> Mean : 24.82 </td> <td> Mean : 28.49 </td> <td> Mean :1.0341 </td> <td> Mean : 815158 </td> <td> Mean :17774 </td> <td> Mean :22373.80 </td> <td> Mean : 0.12675 </td> </tr>
## <tr> <td align="right"> 5 </td> <td> 3rd Qu.:2014-06-29 </td> <td> 3rd Qu.:1520 </td> <td> 3rd Qu.:1584.4 </td> <td> 3rd Qu.: 182.97 </td> <td> 3rd Qu.: 38.00 </td> <td> 3rd Qu.:1.2596 </td> <td> 3rd Qu.:1209336 </td> <td> 3rd Qu.:24086 </td> <td> 3rd Qu.:28686.30 </td> <td> 3rd Qu.: 0.03405 </td> </tr>
## <tr> <td align="right"> 6 </td> <td> Max. :2015-07-19 </td> <td> Max. :6108 </td> <td> Max. :6613.1 </td> <td> Max. : 3005.05 </td> <td> Max. :148.00 </td> <td> Max. :2.2033 </td> <td> Max. :1663757 </td> <td> Max. :38113 </td> <td> Max. :42712.54 </td> <td> Max. :114.00000 </td> </tr>
## </table>
summary(BR) %>% formattable ## DateUS Stakes Return
## "Min. :2011-01-07 " "Min. : 4 " "Min. : 0.0 "
## "1st Qu.:2012-04-01 " "1st Qu.: 328 " "1st Qu.: 293.5 "
## "Median :2013-06-04 " "Median : 759 " "Median : 773.2 "
## "Mean :2013-05-13 " "Mean :1155 " "Mean :1179.4 "
## "3rd Qu.:2014-06-29 " "3rd Qu.:1520 " "3rd Qu.:1584.4 "
## "Max. :2015-07-19 " "Max. :6108 " "Max. :6613.1 "
## PL n rRates
## "Min. :-2527.41 " "Min. : 1.00 " "Min. :0.0000 "
## "1st Qu.: -126.69 " "1st Qu.: 9.00 " "1st Qu.:0.8128 "
## "Median : 14.39 " "Median : 19.00 " "Median :1.0240 "
## "Mean : 24.82 " "Mean : 28.49 " "Mean :1.0341 "
## "3rd Qu.: 182.97 " "3rd Qu.: 38.00 " "3rd Qu.:1.2596 "
## "Max. : 3005.05 " "Max. :148.00 " "Max. :2.2033 "
## CumStakes SPL BR
## "Min. : 40 " "Min. :-4587 " "Min. : 13.05 "
## "1st Qu.: 384506 " "1st Qu.:10615 " "1st Qu.:15215.39 "
## "Median : 827028 " "Median :19262 " "Median :23862.28 "
## "Mean : 815158 " "Mean :17774 " "Mean :22373.80 "
## "3rd Qu.:1209336 " "3rd Qu.:24086 " "3rd Qu.:28686.30 "
## "Max. :1663757 " "Max. :38113 " "Max. :42712.54 "
## gRates
## "Min. : 0.00008 "
## "1st Qu.: 0.02431 "
## "Median : 0.02683 "
## "Mean : 0.12675 "
## "3rd Qu.: 0.03405 "
## "Max. :114.00000 "
summary(BR) %>% formattable %>% as.htmlwidgetkable(summary(BR))| DateUS | Stakes | Return | PL | n | rRates | CumStakes | SPL | BR | gRates | |
|---|---|---|---|---|---|---|---|---|---|---|
| Min. :2011-01-07 | Min. : 4 | Min. : 0.0 | Min. :-2527.41 | Min. : 1.00 | Min. :0.0000 | Min. : 40 | Min. :-4587 | Min. : 13.05 | Min. : 0.00008 | |
| 1st Qu.:2012-04-01 | 1st Qu.: 328 | 1st Qu.: 293.5 | 1st Qu.: -126.69 | 1st Qu.: 9.00 | 1st Qu.:0.8128 | 1st Qu.: 384506 | 1st Qu.:10615 | 1st Qu.:15215.39 | 1st Qu.: 0.02431 | |
| Median :2013-06-04 | Median : 759 | Median : 773.2 | Median : 14.39 | Median : 19.00 | Median :1.0240 | Median : 827028 | Median :19262 | Median :23862.28 | Median : 0.02683 | |
| Mean :2013-05-13 | Mean :1155 | Mean :1179.4 | Mean : 24.82 | Mean : 28.49 | Mean :1.0341 | Mean : 815158 | Mean :17774 | Mean :22373.80 | Mean : 0.12675 | |
| 3rd Qu.:2014-06-29 | 3rd Qu.:1520 | 3rd Qu.:1584.4 | 3rd Qu.: 182.97 | 3rd Qu.: 38.00 | 3rd Qu.:1.2596 | 3rd Qu.:1209336 | 3rd Qu.:24086 | 3rd Qu.:28686.30 | 3rd Qu.: 0.03405 | |
| Max. :2015-07-19 | Max. :6108 | Max. :6613.1 | Max. : 3005.05 | Max. :148.00 | Max. :2.2033 | Max. :1663757 | Max. :38113 | Max. :42712.54 | Max. :114.00000 |
htmlTable(summary(BR))
|
|
|
|
|
| CumStakes |
|
|
| |
|---|---|---|---|---|---|---|---|---|---|---|
| Min. :2011-01-07 | Min. : 4 | Min. : 0.0 | Min. :-2527.41 | Min. : 1.00 | Min. :0.0000 | Min. : 40 | Min. :-4587 | Min. : 13.05 | Min. : 0.00008 | |
| 1st Qu.:2012-04-01 | 1st Qu.: 328 | 1st Qu.: 293.5 | 1st Qu.: -126.69 | 1st Qu.: 9.00 | 1st Qu.:0.8128 | 1st Qu.: 384506 | 1st Qu.:10615 | 1st Qu.:15215.39 | 1st Qu.: 0.02431 | |
| Median :2013-06-04 | Median : 759 | Median : 773.2 | Median : 14.39 | Median : 19.00 | Median :1.0240 | Median : 827028 | Median :19262 | Median :23862.28 | Median : 0.02683 | |
| Mean :2013-05-13 | Mean :1155 | Mean :1179.4 | Mean : 24.82 | Mean : 28.49 | Mean :1.0341 | Mean : 815158 | Mean :17774 | Mean :22373.80 | Mean : 0.12675 | |
| 3rd Qu.:2014-06-29 | 3rd Qu.:1520 | 3rd Qu.:1584.4 | 3rd Qu.: 182.97 | 3rd Qu.: 38.00 | 3rd Qu.:1.2596 | 3rd Qu.:1209336 | 3rd Qu.:24086 | 3rd Qu.:28686.30 | 3rd Qu.: 0.03405 | |
| Max. :2015-07-19 | Max. :6108 | Max. :6613.1 | Max. : 3005.05 | Max. :148.00 | Max. :2.2033 | Max. :1663757 | Max. :38113 | Max. :42712.54 | Max. :114.00000 |
print(xtable(summary(BR)), type = 'html')## <!-- html table generated in R 3.3.2 by xtable 1.8-2 package -->
## <!-- Tue Dec 27 17:34:54 2016 -->
## <table border=1>
## <tr> <th> </th> <th> DateUS </th> <th> Stakes </th> <th> Return </th> <th> PL </th> <th> n </th> <th> rRates </th> <th> CumStakes </th> <th> SPL </th> <th> BR </th> <th> gRates </th> </tr>
## <tr> <td align="right"> 1 </td> <td> Min. :2011-01-07 </td> <td> Min. : 4 </td> <td> Min. : 0.0 </td> <td> Min. :-2527.41 </td> <td> Min. : 1.00 </td> <td> Min. :0.0000 </td> <td> Min. : 40 </td> <td> Min. :-4587 </td> <td> Min. : 13.05 </td> <td> Min. : 0.00008 </td> </tr>
## <tr> <td align="right"> 2 </td> <td> 1st Qu.:2012-04-01 </td> <td> 1st Qu.: 328 </td> <td> 1st Qu.: 293.5 </td> <td> 1st Qu.: -126.69 </td> <td> 1st Qu.: 9.00 </td> <td> 1st Qu.:0.8128 </td> <td> 1st Qu.: 384506 </td> <td> 1st Qu.:10615 </td> <td> 1st Qu.:15215.39 </td> <td> 1st Qu.: 0.02431 </td> </tr>
## <tr> <td align="right"> 3 </td> <td> Median :2013-06-04 </td> <td> Median : 759 </td> <td> Median : 773.2 </td> <td> Median : 14.39 </td> <td> Median : 19.00 </td> <td> Median :1.0240 </td> <td> Median : 827028 </td> <td> Median :19262 </td> <td> Median :23862.28 </td> <td> Median : 0.02683 </td> </tr>
## <tr> <td align="right"> 4 </td> <td> Mean :2013-05-13 </td> <td> Mean :1155 </td> <td> Mean :1179.4 </td> <td> Mean : 24.82 </td> <td> Mean : 28.49 </td> <td> Mean :1.0341 </td> <td> Mean : 815158 </td> <td> Mean :17774 </td> <td> Mean :22373.80 </td> <td> Mean : 0.12675 </td> </tr>
## <tr> <td align="right"> 5 </td> <td> 3rd Qu.:2014-06-29 </td> <td> 3rd Qu.:1520 </td> <td> 3rd Qu.:1584.4 </td> <td> 3rd Qu.: 182.97 </td> <td> 3rd Qu.: 38.00 </td> <td> 3rd Qu.:1.2596 </td> <td> 3rd Qu.:1209336 </td> <td> 3rd Qu.:24086 </td> <td> 3rd Qu.:28686.30 </td> <td> 3rd Qu.: 0.03405 </td> </tr>
## <tr> <td align="right"> 6 </td> <td> Max. :2015-07-19 </td> <td> Max. :6108 </td> <td> Max. :6613.1 </td> <td> Max. : 3005.05 </td> <td> Max. :148.00 </td> <td> Max. :2.2033 </td> <td> Max. :1663757 </td> <td> Max. :38113 </td> <td> Max. :42712.54 </td> <td> Max. :114.00000 </td> </tr>
## </table>
包括数据集抓取、存储、读取、时间序列、数据处理等,涉及R包
包括金融市场的技术指标的各种计算方法,涉及R包
包括金融数据建模,并验证用历史数据验证模型的可靠性,涉及R包
对多策略或多模型进行管理和优化,涉及R包
对持仓进行风险指标的计算和风险提示,涉及R包 1. PerformanceAnalytics (风险分析) 2. fPortfolio (Rmetrics系组合优化) 3. fExtremes (Rmetrics系数据处理)
(I am moving this from the comments to the answer portion of stackoverflow at the request of the original poster.)
There is an article in R News 4/1 (“R Help Desk”, p. 29) that specifically compares Date, POSIXct and chron. (The 1st two are in core of R and chron is a package.)
timeDate class (in the timeDate package) is based on POSIXct but has extra time zone/financial center support.tis package supports many notions of dates.mondate package supports accounting dates.zoo time series package supports just about any date/time class and also has yearmon and yearqtr for ts compatibility.xts time series package works on top of zoo and handles the most common date/time classes by translating them to POSIXct and back again.Reference : Which R time/date class and package to use?
In my first time ploting graph in Betting Strategy and Model Validation, I use ggplot2 with some ggplotthemes and directlable etc to enhance the data visualization effects but later on I use interactive JS plot. Below r packages are useful for plot graph.
highcharter - An interative d3 chart
Unable display hight at html format states the issue but SOLVED as we can see from below data visualization.
suppressAll(library('highcharter'))
suppressAll(source('./function/compareKelly.R'))
## Applied various fractional Kelly models version I.
## stakes based reverser models.
if(file.exists('./data/K1D1.rds')) {
K1D1 <- read_rds(path = './data/K1D1.rds')
} else {
K1D1 <- vKelly(dat, type = 'D1')
}
plotFund <- compareKelly(K1D1, chart = TRUE, type = 'single')
plotFund$Fund.Kelly1.netEMEdge
$Fund.Kelly1.PropHKPriceEdge
$Fund.Kelly1.PropnetProbBEdge
$Fund.Kelly1.KProbHKPrice
$Fund.Kelly1.KProbnetProbB
$Fund.Kelly1.KProbFixed
$Fund.Kelly1.KProbFixednetProbB
$Fund.Kelly1.KEMProb
$Fund.Kelly1.KEMProbnetProbB
$Fund.Kelly1.KProbHalf
$Fund.Kelly1.KProbHalfnetProbB
$Fund.Kelly1.KProbQuarter
$Fund.Kelly1.KProbQuarternetProbB
$Fund.Kelly1.KProbAdj
$Fund.Kelly1.KProbAdjnetProbB
$Fund.Kelly1.KHalfAdj
$Fund.Kelly1.KHalfAdjnetProbB
$Fund.Kelly1.KEMQuarterAdj
$Fund.Kelly1.KEMQuarterAdjnetProbB
$Fund.Kelly2.netEMEdge
$Fund.Kelly2.PropHKPriceEdge
$Fund.Kelly2.PropnetProbBEdge
$Fund.Kelly2.KProbHKPrice
$Fund.Kelly2.KProbnetProbB
$Fund.Kelly2.KProbFixed
$Fund.Kelly2.KProbFixednetProbB
$Fund.Kelly2.KEMProb
$Fund.Kelly2.KEMProbnetProbB
$Fund.Kelly2.KProbHalf
$Fund.Kelly2.KProbHalfnetProbB
$Fund.Kelly2.KProbQuarter
$Fund.Kelly2.KProbQuarternetProbB
$Fund.Kelly2.KProbAdj
$Fund.Kelly2.KProbAdjnetProbB
$Fund.Kelly2.KHalfAdj
$Fund.Kelly2.KHalfAdjnetProbB
$Fund.Kelly2.KEMQuarterAdj
$Fund.Kelly2.KEMQuarterAdjnetProbB
$Fund.Kelly3.netEMEdge
$Fund.Kelly3.PropHKPriceEdge
$Fund.Kelly3.PropnetProbBEdge
$Fund.Kelly3.KProbHKPrice
$Fund.Kelly3.KProbnetProbB
$Fund.Kelly3.KProbFixed
$Fund.Kelly3.KProbFixednetProbB
$Fund.Kelly3.KEMProb
$Fund.Kelly3.KEMProbnetProbB
$Fund.Kelly3.KProbHalf
$Fund.Kelly3.KProbHalfnetProbB
$Fund.Kelly3.KProbQuarter
$Fund.Kelly3.KProbQuarternetProbB
$Fund.Kelly3.KProbAdj
$Fund.Kelly3.KProbAdjnetProbB
$Fund.Kelly3.KHalfAdj
$Fund.Kelly3.KHalfAdjnetProbB
$Fund.Kelly3.KEMQuarterAdj
$Fund.Kelly3.KEMQuarterAdjnetProbB
$Fund.Kelly4.netEMEdge
$Fund.Kelly4.PropHKPriceEdge
$Fund.Kelly4.PropnetProbBEdge
$Fund.Kelly4.KProbHKPrice
$Fund.Kelly4.KProbnetProbB
$Fund.Kelly4.KProbFixed
$Fund.Kelly4.KProbFixednetProbB
$Fund.Kelly4.KEMProb
$Fund.Kelly4.KEMProbnetProbB
$Fund.Kelly4.KProbHalf
$Fund.Kelly4.KProbHalfnetProbB
$Fund.Kelly4.KProbQuarter
$Fund.Kelly4.KProbQuarternetProbB
$Fund.Kelly4.KProbAdj
$Fund.Kelly4.KProbAdjnetProbB
$Fund.Kelly4.KHalfAdj
$Fund.Kelly4.KHalfAdjnetProbB
$Fund.Kelly4.KEMQuarterAdj
$Fund.Kelly4.KEMQuarterAdjnetProbB
graph 2.5.1 : Candle stick chart.
suppressAll(library('highcharter'))
suppressAll(source('./function/compareKelly.R'))
plotFund <- compareKelly(K1D1, chart = TRUE, type = 'multiple')
plotFundgraph 2.5.2 : Comparison of multiple funds chart.
## Need to correct the content, below just a sample...
## Random pick 2 observations among each stratified levels of Result.
K <- llply(split(dat, dat$Result), function(x) x[sample(nrow(x), 2), c('Result', 'Return', 'EUPrice', 'HKPrice', 'Stakes', 'Rebates')]) %>% ldply(., .id = 'Result') %>% mutate(KReturn = currency(Return), Return = currency(Return), KStakes = currency(Stakes), Stakes = currency(Stakes), Rebates = percent(Rebates), Change = percent(KStakes / Stakes - 1)) %>% .[sample(nrow(.), nrow(.)), ]
## http://www.w3schools.com/colors/colors_picker.asp
tagList(
tags$div(align = "center",
class = "bg-info",
tags$h3(class = "bg-primary", "Comparison of Return of Staking"),
tags$h5(align = "center", class = "text-muted",
"Difference of PL btw Normal and Kelly Staking. (Sample)")),
as.htmlwidget(K %>% formattable(list(
Result = formatter('span', style = x ~ ifelse(x == 'Win', style(color = '#269900', font.weight = 'bold'), ifelse(x == 'Half Win', style(color = '#40FF00'), ifelse(x == 'Push', style(color = '#FFFF00'), ifelse(x == 'Half Loss', style(color = '#FF8C1A'), ifelse(x == 'Loss', style(color = '#FF0000', font.weight = 'bold'), NA)))))),
KReturn = formatter('span', style = ~ style(color = ifelse(KReturn >= KStakes, 'green', 'red')), ~ icontext(ifelse(KReturn >= KStakes, 'plus-sign', 'minus-sign'), KReturn)),
Return = formatter('span', style = ~ style(color = ifelse(Return >= Stakes, 'green', 'red')), ~ icontext(ifelse(Return >= Stakes, 'plus-sign', 'minus-sign'), Return)),
EUPrice = color_tile('white', '#003D99'),
HKPrice = color_tile('white', '#003D99'),
KStakes = color_tile('white', '#CC9900'),
Stakes = color_tile('white', '#CC9900'),
Change = formatter('span', style = ~ style(color = ifelse(Change < 0, 'red', 'green')), ~ icontext(ifelse(Change < 0, 'arrow-down', 'arrow-up'), Change))))))table 2.5.1 : Price flutuation table.
library("shiny")
library("highcharter")
data(citytemp)
ui <- fluidPage(
h1("Highcharter Demo"),
fluidRow(
column(width = 4, class = "panel",
selectInput("type", label = "Type", width = "100%",
choices = c("line", "column", "bar", "spline"),
selected = "bar"),
selectInput("stacked", label = "Stacked", width = "100%",
choices = c(FALSE, "normal", "percent"),
selected = "percent"),
selectInput("hc_theme", label = "Theme", width = "100%",
choices = c("538" = "hc_theme_538()", "economist" = "hc_theme_economist()",
"darkunica" = "hc_theme_darkunica()",
"gridlight" = "hc_theme_gridlight()",
"sandsignika" = "hc_theme_sandsignika()",
"null" = "hc_theme_null()", "handdrwran" = "hc_theme_handdrawn()",
"chalk" = "hc_theme_chalk"),
selected = "hc_theme_economist()")),
column(width = 8, highchartOutput("hcontainer",height = "500px"))))
server = function(input, output) {
output$hcontainer <- renderHighchart({
hc <- paste("hc <- highchart() %>%
hc_chart(type = input$type) %>%
hc_plotOptions(
series = list(showInLegend = TRUE)) %>%
hc_add_series(data = list(
list(y = 3, name = \"cat 1\"),
list(y = 4, name = \"cat 2\"))) %>%
hc_add_theme(", input$hc_theme, ")
if (input$stacked != FALSE) {
hc <- hc %>%
hc_plotOptions(showInLegend=TRUE,dataLabels=FALSE)
}")
eval(parse(text = hc))
})}
shinyApp(ui = ui, server = server)## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Reference : - Interactive visualizations with R - a minireview - Interactive DataViz with R - Coursera Data Mining (in my previous assignments, there are some r useful packages like DT, rCharts::dTable() and formattable)
Also other libraries for creating interactive visualizations from R do exist, such as clickme, RIGHT, ggobi, iplots, gg2v, rVega, cranvas and r2d3. Some of these are not under active development anymore. I might include some of those into the comparison here in the future. The d3Network package is also worth checking if you need cool interactive network visualizations.
From the research, I learned from some articles which compare the efficiency of data measurement which will apply in future data analyse and data mining etc.